/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.relatorio;

import br.com.deployment.daoImpl.QuestionnaireResponseDAOImpl;
import br.com.deployment.entities.QuestionnaireResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerRelConclusion {
    
    @EJB
    QuestionnaireResponseDAOImpl quesService;
    
    private List<QuestionnaireResponse> responses;
    
    public ManagerRelConclusion(){}
    
    public void generetorReportPdf(QuestionnaireResponse quesResponse) throws JRException, IOException {
         
        HashMap<String, Object> h = new HashMap<String, Object>();
        
        responses = quesService.findQuestionnaireResponse(quesResponse);
        
        JRBeanCollectionDataSource collecion = new JRBeanCollectionDataSource(responses);
        
        String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/relatorio/relConclusion.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, h, collecion);
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=edital.pdf");
        ServletOutputStream serveOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, serveOutputStream);
        FacesContext.getCurrentInstance().responseComplete();

    }
    
    
}
